package com.ruoyi.web.controller.datastatistics;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datastatistics.domain.TerminalsMonfreezeDb;
import com.ruoyi.datastatistics.service.ITerminalsMonfreezeDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 大表月用量数据Controller
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/datastatistics/terminalsmonfreezedb")
public class TerminalsMonfreezeDbController extends BaseController {
    @Autowired
    private ITerminalsMonfreezeDbService terminalsMonfreezeDbService;

    /**
     * 查询大表月用量数据列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminal:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Map<String, Object> paramMap) {
        startPage();
        List<TerminalsMonfreezeDb> list = terminalsMonfreezeDbService.selectTerminalsMonfreezeDbList(paramMap);
        return getDataTable(list);
    }

    /**
     * 导出大表月用量数据列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminal:list')")
    @Log(title = "大表月用量数据", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMonth")
    public void exportMonth(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        List<TerminalsMonfreezeDb> list = terminalsMonfreezeDbService.selectTerminalsMonfreezeDbList(paramMap);
        ExcelUtil<TerminalsMonfreezeDb> util = new ExcelUtil<TerminalsMonfreezeDb>(TerminalsMonfreezeDb.class);
        util.exportExcel(response, list, "大表月用量数据数据");
    }

    /**
     * 获取大表月用量数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsmonfreezedb:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id) {
        return success(terminalsMonfreezeDbService.selectTerminalsMonfreezeDbById(Id));
    }

    /**
     * 新增大表月用量数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsmonfreezedb:add')")
    @Log(title = "大表月用量数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TerminalsMonfreezeDb terminalsMonfreezeDb) {
        return toAjax(terminalsMonfreezeDbService.insertTerminalsMonfreezeDb(terminalsMonfreezeDb));
    }

    /**
     * 修改大表月用量数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsmonfreezedb:edit')")
    @Log(title = "大表月用量数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TerminalsMonfreezeDb terminalsMonfreezeDb) {
        return toAjax(terminalsMonfreezeDbService.updateTerminalsMonfreezeDb(terminalsMonfreezeDb));
    }

    /**
     * 删除大表月用量数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsmonfreezedb:remove')")
    @Log(title = "大表月用量数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(terminalsMonfreezeDbService.deleteTerminalsMonfreezeDbByIds(Ids));
    }

    /**
     * 大表综合报表月数据
     *
     * @param terminalsMonfreezeDb 查询条件
     * @return AjaxResult
     */
    @GetMapping("/getReportByMonth")
    public TableDataInfo getReportByMonth(TerminalsMonfreezeDb terminalsMonfreezeDb) {
        SysUser user = getLoginUser().getUser();
        if (user.getSystemFlag() != 1 && user.getSystemFlag() != 0) {
            Long userId = user.getUserId();
            terminalsMonfreezeDb.getParams().put("sysUserId", userId);
        }
        List<TerminalsMonfreezeDb> list = terminalsMonfreezeDbService.reportListByMonth(terminalsMonfreezeDb);
        return getDataTable(list);
    }

    /**
     * 大表综合报表月数据导出
     *
     * @param terminalsMonfreezeDb 查询条件
     */
    @PostMapping("/exportReportByMonth")
    public void exportReportByMonth(TerminalsMonfreezeDb terminalsMonfreezeDb, HttpServletResponse response) {
        SysUser user = getLoginUser().getUser();
        if (user.getSystemFlag() != 1 && user.getSystemFlag() != 0) {
            Long userId = user.getUserId();
            terminalsMonfreezeDb.getParams().put("sysUserId", userId);
        }
        if (!StringUtils.isAllBlank(terminalsMonfreezeDb.getStartTime(), terminalsMonfreezeDb.getEndTime())){
            terminalsMonfreezeDb.setFreezeTime(null);
        }
        terminalsMonfreezeDb.getParams().put("export", true);
        List<TerminalsMonfreezeDb> list = terminalsMonfreezeDbService.reportListByMonth(terminalsMonfreezeDb);
        ExcelUtil<TerminalsMonfreezeDb> util = new ExcelUtil<>(TerminalsMonfreezeDb.class);
        util.exportExcel(response, list, "综合报表月数据");
    }

    /**
     * 大表综合报年数据
     *
     * @param terminalsMonfreezeDb 查询条件
     * @return AjaxResult
     */
    @GetMapping("/getReportByYear")
    public TableDataInfo getReportByYear(TerminalsMonfreezeDb terminalsMonfreezeDb) {
        SysUser user = getLoginUser().getUser();
        if (user.getSystemFlag() != 1 && user.getSystemFlag() != 0) {
            Long userId = user.getUserId();
            terminalsMonfreezeDb.getParams().put("sysUserId", userId);
        }
        List<TerminalsMonfreezeDb> list = terminalsMonfreezeDbService.reportListByYear(terminalsMonfreezeDb);
        return getDataTable(list);
    }

    /**
     * 大表综合报表年数据导出
     *
     * @param terminalsMonfreezeDb 查询条件
     */
    @PostMapping("/exportReportByYear")
    public void exportReportByYear(TerminalsMonfreezeDb terminalsMonfreezeDb, HttpServletResponse response) {
        SysUser user = getLoginUser().getUser();
        if (user.getSystemFlag() != 1 && user.getSystemFlag() != 0) {
            Long userId = user.getUserId();
            terminalsMonfreezeDb.getParams().put("sysUserId", userId);
        }
        terminalsMonfreezeDb.getParams().put("export", true);
        List<TerminalsMonfreezeDb> list = terminalsMonfreezeDbService.reportListByYear(terminalsMonfreezeDb);
        ExcelUtil<TerminalsMonfreezeDb> util = new ExcelUtil<>(TerminalsMonfreezeDb.class);
        util.exportExcel(response, list, "综合报表年数据");
    }
}
